#include <string>
#include <cstdio>
#include <regex>
#include <vector>

bool test_regex(const std::string& line, const std::regex& assembly_pattern, std::vector<std::string> check) {
    std::smatch matches;
    if (std::regex_match(line, matches, assembly_pattern)) {
        if (check.size() == matches.size()) {
            // 替换 C++20 的 ranges::zip 为传统循环
            bool allMatch = true;
            for (size_t i = 0; i < check.size(); ++i) {
                if (check[i] != matches[i].str()) {
                    printf("check    fail!!! line: %s\n", line.c_str());
                    allMatch = false;
                    break;
                }
            }
            if (allMatch) {
                printf("OK       !!!\n");
                return true;
            }
            return false;
        }
        printf("size     !!! line: %s\n", line.c_str());
        return false;
    }
    else {
        printf("match    fail!!! line: %s\n", line.c_str());
        return false;
    }
}

int main() {
    std::regex assembly_pattern(R"(^\s*([0-9a-zA-Z_]+)\s+(.*?)\s*\/\/\s*([0-9a-fA-F]{12}):\s([0-9a-fA-F]{8}).*)");
    {
        std::string line = "\ts_cbranch_execz 556                                        // 000000001040: BF88022C <fsfe>";
        test_regex(line, assembly_pattern, std::vector<std::string>{
            line,
            "s_cbranch_execz",
            "556",
            "000000001040",
            "BF88022C",
        });
    }
    {
        std::string line = "\ts_cbranch_execz 556                                        // 000000001040: BF88022C";
        test_regex(line, assembly_pattern, std::vector<std::string>{
            line,
            "s_cbranch_execz",
            "556",
            "000000001040",
            "BF88022C",
            });
    }
    {
        std::string line = "\ts_cbranch_execz 55[fds]6                                        // 000000001040: BF88022C";
        test_regex(line, assembly_pattern, std::vector<std::string>{
            line,
            "s_cbranch_execz",
            "55[fds]6",
            "000000001040",
            "BF88022C",
            });
    }
    {
        std::string line = "\ts_cbranch_execz 55[fds]6                                        // 000000001040: BF88022C BFc8022C";
        test_regex(line, assembly_pattern, std::vector<std::string>{
            line,
            "s_cbranch_execz",
            "55[fds]6",
            "000000001040",
            "BF88022C",
            });
    }
    {
        std::string line = "\ts_cbranch_execz 55[fd()s]6// 000000001040: BF88022C BFc8022C";
        test_regex(line, assembly_pattern, std::vector<std::string>{
            line,
            "s_cbranch_execz",
            "55[fd()s]6",
            "000000001040",
            "BF88022C",
            });
    }
    {
        std::string line = "\ts_cbranch_execz 55[fd()s]6// 000000001040: BF88022C";
        test_regex(line, assembly_pattern, std::vector<std::string>{
            line,
            "s_cbranch_execz",
            "55[fd()s]6",
            "000000001040",
            "BF88022C",
            });
    }
    {
        std::string line = "\ts_cbranch_execz 556                                        // 000000001040: BF88022C <_Z9SumArraysPfS_S_i+0x8f4>";
        test_regex(line, assembly_pattern, std::vector<std::string>{
            line,
            "s_cbranch_execz",
            "556",
            "000000001040",
            "BF88022C",
            });
    }
    {
        std::string line = "\ts_cbranch_execz 556                                        // 000000001040: BF88022C <_Z9SumArraysPfS_S_i+0x8f4>";
        test_regex(line, assembly_pattern, std::vector<std::string>{
            line,
            "s_cbranch_execz",
            "556",
            "000000001040",
            "BF88022C",
            });
    }
    return 0;
}
